/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TExePos;
import com.ruipengkj.bean.TExePosCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.service.TExePosCatalogService;
import com.ruipengkj.service.TExePosService;
/**
 * 说明：
 * 添加实施场所界面
 * @Author：
 * @Date：2012-03-22
 */
public class DataExePosAddOrModifyDialog extends IDialog implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent ShortNum;//短编号
	private JLabelJTextFieldComponent Name;//场所名称VARCHAR(18)
	private JLabelJTextFieldComponent PersonCapity;//可容纳人数
	private JLabelJTextFieldComponent MinMoney;//最低消费DECIMAL(20,3)
	
	private JLabelJComboBoxComponent PosCatalog;//场所类型
	private JLabelJTextAreaComponent Des;//说明VARCHAR(512)
	private JCheckBox CanBook;//是否可预订
	
	private List<TExePosCatalog> tExePosCatalogList=null;
	
	private JButton sureButton;
	private JButton cancelButton;
	
	private TExePos tExePos=null;//实施场所
	public boolean isSuccess=false;
	
	TExePosService tExePosService = BuildSpringApplication.getInstance().getTExePosServiceImpl();
	TExePosCatalogService tExePosCatalogService = BuildSpringApplication.getInstance().getTExePosCatalogServiceImpl();
	
	public DataExePosAddOrModifyDialog(JDialog jf,String title,boolean b) {
		super(jf, title, b);
		this.setSize(360,500);
		this.setLocationRelativeTo(null);
		this.initComponents();
		this.initListener();
	}
	
	private void initComponents() {
		JPanel main_jp = new BorderJPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		ShortNum = new JLabelJTextFieldComponent("场所短编号：", 30);//短编号
		ShortNum.getText().setEditable(false);
		Name = new JLabelJTextFieldComponent("  场所名称：", 30);//场所名称VARCHAR(18)
		PersonCapity = new JLabelJTextFieldComponent("可容纳人数：", 30);//可容纳人数
		MinMoney = new JLabelJTextFieldComponent("  最低消费：", 30);//最低消费DECIMAL(20,3)
		PosCatalog = new JLabelJComboBoxComponent("  场所类型：", new String[]{"请选择场所类型"}, 188);//场所类型
		
		Des = new JLabelJTextAreaComponent("   说   明：", 30, 5);//说明VARCHAR(512)
		
		CanBook = uiFactory.createJCheckBox("                               ", true);//是否可预订
		CanBook.setFocusable(false);
		JPanel jp = new JPanelFlowLayoutComponent(new Component[]{
				new JLabel("  可 预 订：", ImageFactory.loadImage(ImageFactory.ICON_4), JLabel.LEFT), CanBook
		}, FlowLayout.LEFT);
		
		JPanel down_jp=uiFactory.createFlowJPanel();
		sureButton=new RPJButtonWhite("保存(Ctrl+S)");
		cancelButton=new RPJButtonWhite("取消(Ctrl+Q)");
		down_jp.add(sureButton);
		down_jp.add(cancelButton);
		down_jp.setBackground(Util.PANEL_COLOR);
		
		main_jp.add(ShortNum);
		main_jp.add(Name);
		main_jp.add(PersonCapity);
		main_jp.add(MinMoney);
		main_jp.add(PosCatalog);
		main_jp.add(jp);
		main_jp.add(Des);
		main_jp.add(down_jp);
		
		this.add(main_jp,BorderLayout.CENTER);
		main_jp.setCursor(Util.CURSOR_DEFAULT);
	}
	
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

	public void showDialog(TExePos tExePos) {
		if (tExePos != null && tExePos.getPosID() != null) {
			this.tExePos = tExePos;
			ShortNum.getText().setText(tExePos.getShortID());//短编号
			Name.getText().setText(tExePos.getName());//场所名称VARCHAR(18)
			PersonCapity.getText().setText(tExePos.getPersonCapity()+"");//可容纳人数
			MinMoney.getText().setText(tExePos.getMinMoney()+"");//最低消费DECIMAL(20,3)
			
			Des.getArea().setText(tExePos.getDes());//说明VARCHAR(512)
			CanBook.setSelected(tExePos.isCanBook());//是否可预订
		}
		
		queryThread.start();
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == sureButton) {
			String name = Name.getText().getText();
			String personCapity = PersonCapity.getText().getText();
			String minMoney = MinMoney.getText().getText();
			String des = Des.getArea().getText();
			String posCatalog=PosCatalog.getBox().getSelectedItem().toString();
			if (name.equals("")) {
				Util.showMessageDialog("场所名称不能为空！");
				Name.getText().requestFocus();
				return;
			}
			if (!personCapity.equals("")&&!MatcherFormatUtil.MatcherPriceScheme(personCapity)) {
				Util.showMessageDialog("可容纳人数只能为整数，且不能大于8位！");
				PersonCapity.getText().requestFocus();
				return;
			} else if (personCapity.equals("")) {
				personCapity = "0";
			}
			if(!minMoney.equals("")&&!MatcherFormatUtil.MatcherPriceScheme(minMoney)){
				Util.showMessageDialog("最低消费小数点左边不能超过10位，右边不能超过2位");
				MinMoney.requestFocus();
				return;
			}else if(minMoney.equals("")){
				minMoney="0.00";
			}
			if(posCatalog.equals("请选择场所类型")){
				JOptionPane.showMessageDialog(null, "请选择场所类型");
				return;
			}
			if(des.length()>512){
				JOptionPane.showMessageDialog(null, "说明不能大于512个字");
				Des.requestFocus();
				return;
			}
			try {
				boolean isNew=false;
				if (tExePos == null) {
					List<TExePos> list = tExePosService.findByNameService(name);
					if (list != null && list.size() != 0) {
						if (Util.showConfirmDialog("存在相同名字的餐桌，确定添加吗？") != 0)
							return;
					}
					
					tExePos = new TExePos();
					tExePos.setPosID(UUID.GenUUID15());
					isNew = true;
					tExePos.setStatus(ApplicationContext.DESK_STATUS1);
				}
				tExePos.setShortID(ShortNum.getText().getText());//短编号
				tExePos.setName(name);//场所名称VARCHAR(18)
				tExePos.setPersonCapity(Integer.parseInt(personCapity));//可容纳人数
				tExePos.setMinMoney(Double.parseDouble(minMoney));//最低消费DECIMAL(20,3)
				tExePos.setTExePosCatalog(getTExePosCatalog(posCatalog));//场所类型
				tExePos.setDes(des);//说明VARCHAR(512)
				tExePos.setCanBook(CanBook.isSelected());//是否可预订
			
			
				tExePosService.saveOrUpdateService(tExePos);
				this.isSuccess=true;
				if(isNew){
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加员工信息");
				}else{
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改员工信息");	
				}
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if (e.getSource() == cancelButton) {
			this.dispose();
		} else {
			super.actionPerformed(e);
		}
	}
	
	private TExePosCatalog getTExePosCatalog(String name){
		if(tExePosCatalogList!=null&&tExePosCatalogList.size()!=0){
			for(int i=0;i<tExePosCatalogList.size();i++){
				TExePosCatalog tExePosCatalog = tExePosCatalogList.get(i);
				if(tExePosCatalog.getName().equals(name)){
					return tExePosCatalog;
				}
			}
		}
		return null;
	}
	
	private Thread queryThread=new Thread() {
		public void run(){
			//获取员工号
			try {
				if (ShortNum.getText().getText().equals("")) {
					String sn = tExePosService.getTExePosShortID();
					ShortNum.getText().setText(sn);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			try {
				tExePosCatalogList=tExePosCatalogService.findService(null);
				if(tExePosCatalogList!=null&&tExePosCatalogList.size()!=0){
					boolean isSelect = false;
					for(int i=0;i<tExePosCatalogList.size();i++){
						PosCatalog.addElement(tExePosCatalogList.get(i).getName());
						if (!isSelect && tExePos != null) {
							TExePosCatalog tExePosCatalog=tExePos.getTExePosCatalog();
							if(tExePosCatalog!=null&&tExePosCatalog.getName().equals(tExePosCatalogList.get(i).getName())){
								PosCatalog.getBox().setSelectedIndex(i+1);
								isSelect = true;
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		DataExePosAddOrModifyDialog dialog = new DataExePosAddOrModifyDialog(null, "添加场所", true);
		dialog.showDialog(null);
	}

}
